package com.jml.header交换机;

import com.jml.rabbitmq.RabbitMQConnection;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.util.HashMap;
import java.util.Map;

public class Procuder {
    public static void main(String[] args) throws Exception {
        //1 创建一个ConnectionFactory, 并进行配置
        //2 通过连接工厂创建连接
        Connection connection = RabbitMQConnection.getConnection();
        //3 通过connection创建一个Channel
        Channel channel = connection.createChannel();
        Map<String, Object> headers = new HashMap<>();
        headers.put("my1", "111");
        headers.put("my2", "222");
        //设置完毕然后才接build()
        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                //常用的模式deliveryMode，2即是持久化投递，1不是持久化
                .deliveryMode(2)
                //设置字符集contentEncoding
                .contentEncoding("UTF-8")
                //设置过期时间expiration，超过时间没消费会被移除
                .expiration("10000")
                //自定义属性headers
                .headers(headers)
                .build();
        //4 通过Channel发送数据
        for(int i=0; i < 5; i++){
            String msg = "Hello RabbitMQ!";
            //1 exchange   2 routingKey
            channel.basicPublish("", "test001", properties, msg.getBytes());
        }
        //5 记得要关闭相关的连接
        channel.close();
        connection.close();
    }
}
